import store from '@/store'
import { usePermissionStore } from './permission'
import { getToken, removeToken, setToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import { login, phoneLogin, getInfo, logout as userLogout, changeRoleApi } from '@/api/user'

export const useUserStore = defineStore('user', () => {
  const token = ref(getToken() || '')
  const roles = ref([])
  const userInfo = ref({})
  const permissions = ref([])

  const setRoles = (value) => {
    roles.value = value
  }

  const setPermissions = (value) => {
    permissions.value = value
  }

  const loginRequest = (loginData) => {
    return new Promise((resolve, reject) => {
      login({
        uuid: loginData.uuid,
        username: loginData.userName,
        password: loginData.password,
        code: loginData.code
      })
        .then(({ data }) => {
          setToken(data)
          token.value = data
          resolve(true)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }

  const smsLoginRequest = (loginData) => {
    return new Promise((resolve, reject) => {
      phoneLogin({
        userName: loginData.userName,
        code: loginData.code
      })
        .then(({ data }) => {
          setToken(data.token)
          token.value = data.token
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }

  const queryUserInfo = () => {
    return new Promise((resolve, reject) => {
      getInfo()
        .then((res) => {
          roles.value = res.data.roles || ['user']
          userInfo.value = res.data
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }

  // const changeRoles = async (role) => {
  //   const newToken = 'token-' + role
  //   token.value = newToken
  //   setToken(newToken)
  //   await getInfo()
  //   const permissionStore = usePermissionStore()
  //   permissionStore.setRoutes(roles.value)
  //   resetRouter()
  //   permissionStore.dynamicRoutes.forEach((item) => {
  //     router.addRoute(item)
  //   })
  // }

  const changeRoles = async (roleId) => {
    await changeRoleApi({ id: roleId })
    const permissionStore = usePermissionStore()
    permissionStore.setRoutes(roles.value)
    resetRouter()
    permissionStore.dynamicRoutes.forEach((item) => {
      router.addRoute(item)
    })
  }

  const logout = () => {
    return new Promise((resolve, reject) => {
      userLogout()
        .then((res) => {
          removeToken()
          token.value = ''
          roles.value = []
          resetRouter()
          resolve(res)
        })
        .catch((error) => {
          reject(error)
        })
    })
  }

  const resetToken = () => {
    removeToken()
    token.value = ''
    roles.value = []
  }

  return {
    token,
    roles,
    userInfo,
    permissions,
    setPermissions,
    setRoles,
    loginRequest,
    smsLoginRequest,
    queryUserInfo,
    changeRoles,
    logout,
    resetToken
  }
})

export function useUserStoreHook() {
  return useUserStore(store)
}
